/*
 * Main.fx
 *
 * Created on 2 Mar, 2009, 8:49:22 PM
 */

package zebbowlingpresentation;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.animation.transition.FadeTransition;
import javafx.scene.Group;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.Scene;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import zebbowlingpresentation.ImageButton;

/**
 * @author TeamTypeZero
 */

var slideURL = "{__DIR__}ZebulonBowlingPresentation/";
var slideCount = 13;

var sceneWidth = 500;
var sceneHeight = 420;
var index = 0;
var maxIndex = slideCount;
var defaultOrder = true;

function getImage(i:Integer) : Image {

    transTransition.stop();
    transTransition.playFromStart();

    var placeHolder = "placeholder";
    if(i == 0) {
        placeHolder = "splash";
    }

    Image {
        url: "{slideURL}Slide{i}.jpg"
        width: sceneWidth
        height: sceneHeight - 45
        backgroundLoading: true
        placeholder: Image {
            url: "{__DIR__}images/{placeHolder}.jpg"
        }
    }
}

var imageView: ImageView[] = [
    ImageView {
        image: Image {
            url: "{__DIR__}images/placeholder.jpg"
        }
    },
    ImageView {
        image: bind getImage(index)
        smooth: true
    }
];

var transTransition = FadeTransition {
    node: bind imageView[1]
    duration: 3s
    fromValue: 0.0
    toValue: 1.0
}

var slideShowTimeline = Timeline {
    repeatCount: Timeline.INDEFINITE
    keyFrames: [
        KeyFrame {
            time: 10s
            action: function() {
                if(index == maxIndex) {
                    index = 0;
                } else {
                    index++;
                }
            }
        }
    ]
}

var bgRect = Rectangle {
    x: 0
    y: sceneHeight - 45
    width: sceneWidth
    height: 45
    fill: LinearGradient {
        startX: 0.5
        startY: 0.0
        endX: 0.5
        endY: 1.0
        proportional: true
        stops: [
            Stop {
                offset: 0.0
                color: Color.GRAY
            },
            Stop {
                offset: 0.6
                color: Color.BLACK
            }
        ]
    }
}

var backButton = ImageButton {

    normalImage: Image {
        url: "{__DIR__}images/back_n.png"
    }
    hoverImage: Image {
        url: "{__DIR__}images/back_h.png"
    }
    translateX: 10
    translateY: sceneHeight - 40

    onMousePressed: function(e) {
        if(index == 0) {
            index = maxIndex;
        } else {
            index--;
        }
    }
}

var nextButton = ImageButton {

    normalImage: Image {
        url: "{__DIR__}images/next_n.png"
    }
    hoverImage: Image {
        url: "{__DIR__}images/next_h.png"
    }
    translateX: sceneWidth - 43
    translateY: sceneHeight - 40

    onMousePressed: function(e) {
        if(index == maxIndex) {
            index = 0;
        } else {
            index++;
        }
    }
}

var playImage = Image {
    url: "{__DIR__}images/play.png"
}
var pauseImage = Image {
    url: "{__DIR__}images/pause.png"
}
function getPlayImage(paused : Boolean) : Image {
    if(paused) {
        return playImage;
    } else {
        return pauseImage;
    }
}
var playButton = ImageView {
    image: bind getPlayImage(slideShowTimeline.paused)
    translateX: (sceneWidth - 42) / 2.0
    translateY: (sceneHeight - 40)
    onMousePressed: function(e) {
        if(slideShowTimeline.paused) {
            slideShowTimeline.play();
        } else {
            slideShowTimeline.pause();
        }
    }
}

Stage {
    title: "Zebulon Bowling Presentation"
    width: sceneWidth
    height: sceneHeight
    resizable: false

    scene: Scene {
        content: Group {
            content: [
                Group {
                    content: imageView
                }, bgRect, backButton, nextButton, playButton
            ]
        }
        fill: Color.BLACK
    }
    style: StageStyle.UNDECORATED
}
slideShowTimeline.play();